<?php
namespace App;
defined('IN_IA') or exit('Access Denied');
use Model\MemberMod;
use Model\RedMod;
use Model\RedRecordMod;
use Model\WqMemberMod;
use Pub\tool\Setting as SettingCache;

class Member extends Action
{
    private $Member = null;
    private $RedRecord = null;
    private $WqMember = null;

    public function __construct()
    {
        parent::__construct();
        $this->Member = new MemberMod();
        $this->getUid();
    }

    /**
     * 注册登录模块返回uid
     */
    public function login()
    {
        $_data = array('uid'=>$this->_uid,'uniacid'=>$this->_uniacid);
        if(!$this->Member->findOne($_data)){
            $_data['register_time'] = TIMESTAMP;
            if($this->Member->insert($_data)){
                $this->_result = array('status'=>'success','data'=>$this->_uid);
            }
        }else{
            $this->_result = array('status'=>'success','data'=>$this->_uid);
        }
        $this->result($this->_result);
    }


    /**
     * 获取指定数据
     */
    public function easyFind()
    {
        $_return = array();
        if(isset($this->_gpc['return'])) $_return = $this->_gpc['return'];
        $_member = $this->Member->findOne(array('uid'=>$this->_uid,'uniacid'=>$this->_uniacid),$_return);
        $_member_info = $this->uidToInfo($this->_uid);
        $_member['nickname'] = $_member_info['nickname'];
        $_member['header'] = $_member_info['avatar'];
        unset($_member_info);
        if($_member["balance"] && SettingCache::get(SettingCache::USE_BALANCE) == 2){
            $_member["balance"] = $this->getWqBalance($this->_uid);
        }
        if($_member) $this->_result = array('status'=>'success','data'=>$_member);
        $this->result($this->_result);
    }

    /**
     * 获取微擎余额
     * @param int $_uid 用户id
     * @return double 用户余额
     */
    private function getWqBalance($_uid)
    {
        $this->WqMember = new WqMemberMod();
        $_member = $this->WqMember->findOne(array('uid'=>$_uid),array("credit2"));
        return $_member["credit2"];
    }

    /**
     * 收入记录
     */
    public function income()
    {
        $this->RedRecord = new RedRecordMod();
        $_list = $this->RedRecord->findAll(
            array('uniacid'=>$this->_uniacid,'uid'=>$this->_uid),
            array('order'=>array('id','desc'),'limit'=>$this->_gpc['limit']),
            array('id','time','red_id','money')
        );
        if(count($_list) > 0){
            foreach($_list as $_k=>$_v){
                $_list[$_k]['time'] = date('Y-m-d H:i:s',$_v['time']);
                $_list[$_k]['type'] = $this->redIdToType($_v['red_id']);
            }
            $this->_result = array('status'=>'success','data'=>$_list);
        }
        $this->result($this->_result);
    }

    /**
     * 知道可以上拉加载更多红包了
     * @param integer uid 用户id
     */
    public function knowMore()
    {
        if($this->Member->update(array("hint_more"=>0),array("uid"=>$this->_uid,"uniacid"=>$this->_uniacid))){
            $this->_result = array("status"=>"success");
        }
        $this->result($this->_result);
    }

    //------------web端----------------
    /**
     * 会员总数
     */
    public function getTotal()
    {
        $this->_result = array("status"=>"success","data"=>$this->Member->total(array("uniacid"=>$this->_uniacid)));
        $this->result($this->_result);
    }

    /**
     * 获取用户列表
     */
    public function getList()
    {
        $_use_balance = SettingCache::get(SettingCache::USE_BALANCE);
        $_return = array("uid","register_time","phone");
        if($_use_balance == 1) $_return[] = "balance";
        $_list = $this->Member->findAll(
            array("uniacid"=>$this->_uniacid),
            array("order"=>array("id","desc"),"limit"=>$this->_gpc["limit"]),
            $_return
        );
        if(count($_list) > 0){
            $Red = new RedMod();
            foreach($_list as $_k=>$_v){
                $_member = $this->uidToInfo($_v["uid"]);
                $_list[$_k]["nickname"] = $_member["nickname"];
                $_list[$_k]["header"] = $_member["avatar"];
                $_list[$_k]["red_number"] = $Red->total(array("uniacid"=>$this->_uniacid,"uid"=>$_v["uid"],"status >"=>1));
                $_list[$_k]["register_time"] = date("Y-m-d H:i",$_v["register_time"]);
                if($_use_balance == 2) $_list[$_k]["balance"] = $this->getWqBalance($_v["uid"]);
            }
            $this->_result = array("status"=>"success","data"=>$_list);
            unset($_member);
        }
        $this->result($this->_result);
    }
}